package com.hbnu.config;
import com.hbnu.util.DrawCode;
import com.hbnu.tourist.domain.Tourist;
import com.hbnu.service.LoginService;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/tourists")

@Slf4j
public class LoginTouristController {
    @Autowired
    private LoginService loginService;
    //测试请求
    @GetMapping("/test")
    public String Test(){
        return "ajaxResult";
    }
    //获取图形验证码请求
    @GetMapping("/captchaImage")
    public AjaxResult login(){
        DrawCode drawCode = new DrawCode();
        Map<String, String> resultMap = drawCode.drawCode();
        log.info("图形验证码：{}", resultMap.get("code"));
        return AjaxResult.success("图形验证码获取成功")
                .put("image", resultMap.get("image"))
                .put("code",200)
                .put("icon", resultMap.get("code"));
    }

    //获取邮箱验证码请求
    @PostMapping("/getCode")
    public AjaxResult getCode(@RequestBody Map<String, String> result){
        log.info("邮箱验证码请求参数：{}", result);
        return loginService.getCode(result.get("email"), result.get("text"));
    }
    //登录请求
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Tourist tourist){
       log.info("登录请求参数：{}", tourist);
        return loginService.login(tourist);
    }
    //注册请求
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Map<String, String> result){
        log.info("注册请求参数：{}", result);
        Tourist tourist = new Tourist();
        tourist.setEmail(result.get("email"));
        tourist.setPassword(result.get("password"));
        String code = result.get("note");
        return loginService.register(tourist,code);
    }
    //找回密码验证验证码请求
    @PostMapping("/findPassword")
    public AjaxResult findPassword(@RequestBody Map<String, String> result){
        log.info("验证密码请求参数：{}", result);
        return loginService.findPassword(result.get("email"), result.get("text"));
    }
    //找回密码请求
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(@RequestBody Map<String, String> result){
        log.info("修改密码请求参数：{}", result);
        Tourist tourist = new Tourist();
        tourist.setEmail(result.get("email"));
        tourist.setPassword(result.get("password"));
        String code = result.get("note");
        return loginService.updatePassword(tourist,code);
    }

    @DeleteMapping("/{id}")
    public AjaxResult login(@PathVariable Integer id){
        return AjaxResult.success(new Tourist());
    }
}
